package com.holiday.springboot2.intercepter;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 拦截器类说明
 *      1、自定义类，并实现HandlerInterceptor，重写三个方法
 *          1、preHandle：调用Controller某个方法之前时候执行，返回 true 则放行，让本次请求往下走，返回false则退出此次请求
 *          2、postHandle：调用Controller某个方法之后时候执行，如果Controller报错了，则不会执行这个方法
 *          3、afterCompletion：调用Controller某个方法不管有没有异常，都会执行这个方法
 */
public class LoginIntercepter implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("---------preHandle");
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("---------postHandle");
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("---------afterCompletion");
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
